package base.Clong;

import com.google.gson.Gson;

public class Cl {
    public static void main(String[] args) throws CloneNotSupportedException {

        //浅克隆
        person p1 = new person("李乐",18, 1);
        person p2 =(person) p1.clone();
        System.out.println(p1);
        System.out.println(p2);
        
        //第三方克隆工具
        Gson gson = new Gson(); //gson对象
        //把对象变成一个字符串
        String ps = gson.toJson(p1);
        System.out.println(ps);//
        //再把字符串变回对象
        person p3 = gson.fromJson(ps, person.class);
        p1.age=12;
        System.out.println(p1);
        System.out.println(p3);

    }
}



class person implements Cloneable{
    String name ;
    int age ;
    int id;

    public person(String name, int age, int id) {
        this.name = name;
        this.age = age;
        this.id = id;
    }

    @Override//重写 原打印对象地址值
    public String toString() {
        return "姓名："+name + ",年龄；" + age + ",id:" + id;
    }

    

    @Override//重写 原权限无法调用
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}
